package com.game.ThauanFramework;

import java.util.HashMap;
import java.util.Set;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;

public class SoundManager {

	private String TAG = "Sound Manager";

	private AudioManager audioManager;

	public AudioManager getAudioManager() {
		audioManager = (AudioManager) MinhasCoisas.getCurrentActivity()
				.getSystemService(Context.AUDIO_SERVICE);
		return audioManager;
	}

	public void setAudioManager(AudioManager audioManager) {
		this.audioManager = audioManager;
	}

	private HashMap<String, MediaPlayer> songs;
	private static SoundManager instance;

	private SoundManager() {
		audioManager = (AudioManager) MinhasCoisas.getCurrentActivity()
				.getSystemService(Context.AUDIO_SERVICE);
		this.songs = new HashMap<String, MediaPlayer>();
	}

	public static SoundManager getInstance() {
		if (instance == null) {
			instance = new SoundManager();
		}

		return instance;
	}

	public void playSound(final int source, final String name) {
		final MediaPlayer mp = MediaPlayer.create(
				MinhasCoisas.getCurrentActivity(), source);
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				Log.i(TAG, "REALEASE");
				mp.release();
				songs.remove(name);
			}
		});

		try {
			mp.start();

			this.songs.put(name, mp);

		} catch (Exception e) {
			mp.stop();
			Log.i(TAG, "Erro no som");
		}
	}

	public void playSound(final int source, final String name, int volume) {
		final MediaPlayer mp = MediaPlayer.create(
				MinhasCoisas.getCurrentActivity(), source);
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				Log.i(TAG, "REALEASE");
				mp.release();
				songs.remove(name);
			}
		});
		try {
			mp.start();
			mp.setVolume(volume, volume);
			this.songs.put(name, mp);

		} catch (Exception e) {
			mp.stop();
			Log.i(TAG, "Erro no som");
		}
	}

	public void playSound(final int source) {
		final MediaPlayer mp = MediaPlayer.create(
				MinhasCoisas.getCurrentActivity(), source);
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				Log.i(TAG, "REALEASE");
				mp.release();
			}
		});
		try {
			mp.start();

		} catch (Exception e) {
			mp.stop();
			Log.i(TAG, "Erro no som");
		}
	}

	public void playLoopingSound(final int source, final String name) {
		if (songs.get(name) != null) {
			songs.get(name).start();
		} else {

			final MediaPlayer mp = MediaPlayer.create(
					MinhasCoisas.getCurrentActivity(), source);

			mp.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					Log.i(TAG, "REALEASE");
					mp.release();
					songs.remove(name);
				}
			});

			try {
				mp.start();

				mp.setLooping(true);
				this.songs.put(name, mp);
			} catch (Exception e) {
				mp.stop();
				Log.i(TAG, "Erro no som");
			}
		}
	}

	public void StopSong(String name) {
		try {
			this.songs.get(name).stop();
		} catch (Exception e) {
			Log.i(TAG, "Erro ao tentar parar som");
		}
	}

	public void StopAllSongs() {
		Set<String> chaves = songs.keySet();
		for (String chave : chaves) {
			if (chave != null)
				songs.get(chave).stop();
		}
	}

}
